package com.power.wx.bean;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.UUID;

import org.apache.log4j.Logger;

import com.power.wx.servlet.WechatServlet;
import com.qq.weixin.mp.aes.AesException;
import com.qq.weixin.mp.aes.WXBizMsgCrypt;

public abstract class Decision {

	private static final Logger logger = Logger.getLogger(WechatServlet.class);

	public static String MsgTypeText = "text";

	private String toUserName;
	private String fromUserName;
	private int createTime;
	private String msgType;

	public Decision() {
	}

	public Decision(String toUserName, String fromUserName, int createTime, String msgType) {
		super();
		this.toUserName = toUserName;
		this.fromUserName = fromUserName;
		this.createTime = createTime;
		this.msgType = msgType;
	}

	public String getToUserName() {
		return toUserName;
	}

	public void setToUserName(String toUserName) {
		this.toUserName = toUserName;
	}

	public String getFromUserName() {
		return fromUserName;
	}

	public void setFromUserName(String fromUserName) {
		this.fromUserName = fromUserName;
	}

	public int getCreateTime() {
		return createTime;
	}

	public void setCreateTime(int createTime) {
		this.createTime = createTime;
	}

	public String getMsgType() {
		return msgType;
	}

	public void setMsgType(String msgType) {
		this.msgType = msgType;
	}

	@Override
	public String toString() {
		return "Decision [toUserName=" + toUserName + ", fromUserName=" + fromUserName + ", createTime=" + createTime + ", msgType=" + msgType + "]";
	}

	public String toXml() {
		StringBuilder sb = new StringBuilder();
		sb.append("<xml>");
		sb.append("<ToUserName><![CDATA[" + toUserName + "]]></ToUserName>");
		sb.append("<FromUserName><![CDATA[" + fromUserName + "]]></FromUserName>");
		sb.append("<CreateTime>" + System.currentTimeMillis() / 1000 + "</CreateTime>");
		sb.append("<MsgType><![CDATA[" + msgType + "]]></MsgType>");
		sb.append(subXML());
		sb.append("</xml>");
		return sb.toString();
	}

	protected abstract String subXML();

	public boolean outputStreamWrite(OutputStream outputStream) {
		try {
			outputStream.write(toXml().getBytes("utf-8"));
			outputStream.flush();
		} catch (UnsupportedEncodingException e) {
			logger.error(e, e);
			return false;
		} catch (IOException e) {
			logger.error(e, e);
			return false;
		}
		return true;
	}

	public boolean outputStreamWrite(OutputStream outputStream, WXBizMsgCrypt bizMsgCrypt) {
		if (bizMsgCrypt != null) {
			try {
				String outputStr = bizMsgCrypt.encryptMsg(toXml(), System.currentTimeMillis() + "", UUID.randomUUID().toString());
				outputStream.write(outputStr.getBytes("utf-8"));
				outputStream.flush();
			} catch (UnsupportedEncodingException e) {
				logger.error(e, e);
				return false;
			} catch (IOException e) {
				logger.error(e, e);
				return false;
			} catch (AesException e) {
				logger.error(e, e);
				return false;
			}
			return true;
		} else {
			return outputStreamWrite(outputStream);
		}
	}

}
